package com.ck.lock.deadlock;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author ck
 * @date 2023/7/4 16:58
 */
public class LockDemoTest {
    private static ReentrantLock lock1 = new ReentrantLock();
    private static ReentrantLock lock2 = new ReentrantLock();


    /**
     *  2、解决办法
     *  利用Lock接口的tryLock方法去获取锁的状态，如已持有锁则返回false，未持有锁则返回true
     *  tryLock() 方法是一个非阻塞方法，它会立即返回获取锁的结果，不会阻塞当前线程。
     *  因此，即使获取锁失败，线程也不会被阻塞，可以根据返回结果进行相应的处理，避免出现死锁情况。
     * @param args
     */
    public static void main(String[] args) {
        Thread thread1 = new Thread(()->{
            try {
                if(lock1.tryLock(1, TimeUnit.SECONDS)){
                    System.out.println(Thread.currentThread().getName()+"获取到了lock1");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    if (lock2.tryLock(5, TimeUnit.SECONDS)){
                        System.out.println(Thread.currentThread().getName()+"获取到了lock2");
                    }
                    else {
                        System.out.println(Thread.currentThread().getName()+"获取lock2锁失败");
                    }
                }
                else {
                    System.out.println(Thread.currentThread().getName()+"获取lock1锁失败");
                }
            } catch (InterruptedException e) {
               e.printStackTrace();
            }
            finally {
                if (lock1.isHeldByCurrentThread()){
                    lock1.unlock();
                }
                if (lock2.isHeldByCurrentThread()){
                    lock2.unlock();
                }
            }
        },"线程1");

        Thread thread2 = new Thread(()->{
            try {
                if (lock2.tryLock(1, TimeUnit.SECONDS)){
                    System.out.println(Thread.currentThread().getName()+"获取到了lock2");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    if(lock1.tryLock(1, TimeUnit.SECONDS)){
                        System.out.println(Thread.currentThread().getName()+"获取到了lock1");
                    }
                    else {
                        System.out.println(Thread.currentThread().getName()+"获取lock1锁失败");
                    }
                }
                else {
                    System.out.println(Thread.currentThread().getName()+"获取lock2锁失败");
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            finally {
                if (lock1.isHeldByCurrentThread()){
                    lock1.unlock();
                }
                if (lock2.isHeldByCurrentThread()){
                    lock2.unlock();
                }
            }
        },"线程2");

        thread1.start();
        thread2.start();
    }
}
